package br.net.woodstock.tickettoride.vaadin.web;

import br.net.woodstock.tickettoride.vaadin.web.access.UserView;
import br.net.woodstock.tickettoride.vaadin.web.manager.ApplicationView;
import br.net.woodstock.tickettoride.vaadin.web.manager.CertificateView;
import br.net.woodstock.tickettoride.vaadin.web.manager.TicketView;

import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Layout;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.Command;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.VerticalLayout;

public class MenuView extends AbstractView {

	private static final long	serialVersionUID	= 6359141818451948198L;

	public MenuView() {
		super();
	}

	@Override
	protected void init() {
		VerticalLayout layout = new VerticalLayout();

		layout.addComponent(this.createMenuBar());

		layout.setWidth(100, Sizeable.UNITS_PERCENTAGE);

		this.addComponent(layout);
		this.setComponentAlignment(layout, Alignment.TOP_CENTER);
	}

	private MenuBar createMenuBar() {
		MenuBar menuBar = new MenuBar();
		MenuItem mainMenu = menuBar.addItem(this.getMessage("label.mainMenu"), null);
		MenuItem mgtMenu = mainMenu.addItem(this.getMessage("label.managment"), null);

		mgtMenu.addItem(this.getMessage("label.application"), MenuView.this.getChangeViewCommand(new ApplicationView()));

		mgtMenu.addItem(this.getMessage("label.certificate"), MenuView.this.getChangeViewCommand(new CertificateView()));

		mgtMenu.addItem(this.getMessage("label.ticket"), MenuView.this.getChangeViewCommand(new TicketView()));

		mgtMenu.addItem(this.getMessage("label.user"), MenuView.this.getChangeViewCommand(new UserView()));

		mainMenu.addItem(this.getMessage("label.logout"), MenuView.this.getLogoutCommand());

		menuBar.setWidth(100, Sizeable.UNITS_PERCENTAGE);
		return menuBar;
	}

	public Command getChangeViewCommand(final Layout layout) {
		return new Command() {

			private static final long	serialVersionUID	= 1L;

			@Override
			public void menuSelected(final MenuItem selectedItem) {
				TicketToRideApplication application = (TicketToRideApplication) MenuView.this.getApplication();
				ViewManager viewManager = application.getViewManager();
				viewManager.switchBody(layout);
			}

		};
	}

	public Command getLogoutCommand() {
		return new Command() {

			private static final long	serialVersionUID	= 1L;

			@Override
			public void menuSelected(final MenuItem selectedItem) {
				TicketToRideApplication application = (TicketToRideApplication) MenuView.this.getApplication();
				application.doLogout();
			}

		};
	}

}
